package com.og.ouroa.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.common.annotations.EnableLoginHandle;
import com.og.ouroa.common.exception.Assert;
import com.og.ouroa.common.exception.BusinessException;
import com.og.ouroa.common.result.R;
import com.og.ouroa.common.result.ResponseEnum;
import com.og.ouroa.pojo.entity.UserInfo;
import com.og.ouroa.pojo.vo.CourseFileVO;
import com.og.ouroa.pojo.vo.LoginVO;
import com.og.ouroa.pojo.vo.RegisterVO;
import com.og.ouroa.pojo.vo.UserInfoVO;
import com.og.ouroa.service.FileLoadService;
import com.og.ouroa.service.UserInfoService;
import com.og.ouroa.utils.JwtUtils;
import com.og.ouroa.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.security.provider.MD5;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/userInfo")
@Api(tags = "用户操作接口")
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    FileLoadService fileLoadService;


    @EnableLoginHandle(identity = UserInfo.TEACHER)
    @ApiOperation("会员注册(老师权限)")
    @PostMapping("/register")
    public R register(@ApiParam("注册对象")
                      @RequestBody RegisterVO registerVO) {


        //重复校验校验

        userInfoService.checkNumber(registerVO.getIdentity(),registerVO.getNumber());

        //注册
        userInfoService.register(registerVO);


        return R.ok().message("注册成功");
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@ApiParam()
                   @RequestBody LoginVO loginVO) {

        UserInfoVO infoVO = userInfoService.login(loginVO);

        return R.ok().message("登录成功").data("infoVO", infoVO);


    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {

        String token = request.getHeader("token");  // 获取令牌(前端将token放入请求头中)


        boolean result = JwtUtils.checkToken(token);

        if (result) {
            return R.ok();
        } else {
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }

    @EnableLoginHandle
    @ApiOperation("头像上传")
    @PostMapping("/upload")
    public R upload(
            @ApiParam(value = "图片文件", required = true)
            @RequestParam("file")
                    MultipartFile file,
            @ApiParam(value = "用户id", required = true)
            @RequestParam("sid")
                    Integer sid) {

        String url = null;
        String Filename;
        try {
            InputStream inputStream = file.getInputStream();
            Filename = file.getOriginalFilename();
            url = fileLoadService.upload(inputStream, "userImg", Filename);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR, e);
        }


        userInfoService.imputUrlbyId(sid, url);


        return R.ok().message("头像上传成功").data("url", url);
    }

    @EnableLoginHandle
    @ApiOperation("修改密码")
    @PostMapping("/UpdatePwd")
    public R UpdatePwd(@ApiParam(value = "用户id", required = true)
                       @RequestParam("sid")
                               Integer sid,
                       @ApiParam(value = "旧密码", required = true)
                       @RequestParam("oldPwd")
                               String oldpwd,
                       @ApiParam(value = "新密码", required = true)
                       @RequestParam("newPwd")
                               String newpwd
    ) {


        UserInfo user = userInfoService.getById(sid);
        System.out.println(MD5Utils.encrypt(oldpwd));System.out.println(user.getPassword());
        Assert.equals(MD5Utils.encrypt(oldpwd), user.getPassword(), ResponseEnum.LOGIN_PASSWORD_ERROR);


        userInfoService.updatePwd(newpwd, sid);


        return R.ok().message("修改成功");
    }

    @EnableLoginHandle
    @ApiOperation("修改基本信息")
    @PostMapping("/update/{sid}")
    public R update(

            @ApiParam(value = "修改后对象", required = true)
                    RegisterVO registerVO,
            @ApiParam(value = "用户id", required = true)
            @RequestParam("sid")
                    Integer sid
    ) {

        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(registerVO, userInfo);
        userInfo.setId(sid);

         registerVO.setPassword(userInfoService.getById(sid).getPassword());

        userInfoService.updateById(userInfo);


        return R.ok().message("修改成功");

    }
    @EnableLoginHandle
    @ApiOperation("查询老师")
    @PostMapping("/listTeacher/{page}/{limit}")
    public R listTeacher(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit
    ) {


         Page<UserInfo> userInfoPage = new Page<>();
         IPage<UserInfo> userInfoIPage= userInfoService.listTeacher(userInfoPage);


        return R.ok().message("查询成功").data("page",userInfoIPage);

    }

    @EnableLoginHandle
    @ApiOperation("获取uid")
    @PostMapping("/listTeacher/{u_number}")
    public R listTeacher(
            @ApiParam(value = "学号", required = true) @PathVariable String u_number
    ) {


         Integer integer = userInfoService.getidByNumber(u_number);


        return R.ok().message("查询成功").data("id",integer);

    }

    @EnableLoginHandle
    @ApiOperation("根据id获取信息")
    @GetMapping("/getByUid/{uid}")
    public R getByUid(@ApiParam(value = "uid", required = true) @PathVariable Integer uid) {


        UserInfo byId = userInfoService.getById(uid);
        return R.ok().message("查询成功").data("userinfo",byId);



    }
}

